<?php
declare (strict_types=1);

namespace app\api\controller\v1;

use app\api\helpers\v1\traits\SearchDataForModel;
use app\api\model\v1\AdminLog;
use app\api\model\v1\AuthWxGroup;
use app\api\model\v1\AuthWxMenu;
use app\api\model\v1\AuthWxStudy;
use app\api\model\v1\Users;
use app\api\model\v1\WxMenu;
use app\BaseController;
use think\Request;

class WxMenuCon extends BaseController
{
    use SearchDataForModel;

    /**
     * Notes:
     * User: liuhailong
     * DateTime: 2021/12/28 10:04
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     * 数据源--PC端
     */
    public function getWxMenuList()
    {
        $limit = $this->request->param('limit', 10);
        $type = $this->request->param('type', 'page');
        if ($type === 'no_page') {
            $res = WxMenu::where(['status' => 1])->order('sort asc')->select();
        } else {
            $res = $this->search(new WxMenu(), [], $limit, 'asc');
        }

        return $this->response($res);
    }

    /**
     * Notes:
     * User: liuhailong
     * DateTime: 2021/12/28 14:24
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 菜单微信端
     */
    public function getWxMenuInfo()
    {
        $data = $this->request->param();
        $user = Users::with(['roles'])->find($data['user_id']);
        if (is_null($user)){
            return $this->response(404,'获取菜单失败');
        }

        $wx_menu_ids = $user['roles']['rules'];
        $wxMenu = WxMenu::whereIn('id',$wx_menu_ids)->select()->toArray();
        return $this->response($wxMenu);

//        halt($user->toArray());
//        $arr = [];
//        if (intval($data['unit_id']) !== 0) {
//            $res = AuthWxMenu::findWxMenu(intval($data['unit_id']), $this->search);
//            foreach ($res as $key => $v) {
//                $arr[] = $v['id'];
//            }
//            $res = AuthWxMenu::with(['wxMenu' => function ($query) {
//                $query->where(['status' => WxMenu::IS_STATUS_ONE, 'is_home' => WxMenu::IS_HOME_ONE]);
//            }])->whereIn('id', $arr)->order('id asc')->select()->filter(function (AuthWxMenu $authWxMenu) {
//                if (!empty($authWxMenu->wxMenu)) {
//                    return true;
//                }
//            })->toArray();
//        } else {
//            $res = AuthWxMenu::with(['wxMenu' => function ($query) {
//                $query->where(['status' => WxMenu::IS_STATUS_ONE, 'is_home' => WxMenu::IS_HOME_ONE]);
//            }])->where('unit_id', 1)->order('id asc')->select()->filter(function (AuthWxMenu $authWxMenu) {
//                if (!empty($authWxMenu->wxMenu)) {
//                    return true;
//                }
//            })->toArray();
//        }
//        return $this->response(array_values($res));
    }

    /**
     * Notes:
     * User: liuhailong
     * DateTime: 2022/4/20 16:51
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 题库
     */
    public function getWxStudyInfo()
    {
        $data = $this->request->param();
        $arr = [];
        if (intval($data['unit_id']) !== 0) {
            $res = AuthWxStudy::findWxStduy(intval($data['unit_id']), $this->search);
            foreach ($res as $key => $v) {
                $arr[] = $v['id'];
            }
            $res = AuthWxStudy::with(['wxStudy'])->whereIn('id', $arr)->order('id asc')->select()->filter(function (AuthWxStudy $authWxStudy) {
                if (!empty($authWxStudy->wxStudy)) {
                    return true;
                }
            })->toArray();
        } else {
            $res = AuthWxMenu::with(['wxStudy'])->where('unit_id', 1)->order('id asc')->select()->filter(function (AuthWxStudy $authWxStudy) {
                if (!empty($authWxStudy->wxStudy)) {
                    return true;
                }
            })->toArray();
        }
        return $this->response(array_values($res));
    }

    /**
     * Notes:
     * User: liuhailong
     * DateTime: 2021/12/28 15:02
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 显示更多
     */
    public function getWxMenuInfoMore()
    {
        $data = $this->request->param();
        $new_arr = [];
        $user = Users::with(['roles'])->find($data['user_id']);
        if (is_null($user)){
            return $this->response(404,'获取菜单失败');
        }

        $wx_menu_ids = $user['roles']['rules'];
        $wxMenu = WxMenu::whereIn('id',$wx_menu_ids)->select()->toArray();
        foreach ($wxMenu as $key => $val) {
            $new_arr[$val['menu_type']]['title'] = WxMenu::$menu_type_arr[$val['menu_type']];
            $new_arr[$val['menu_type']]['child'][] = $val;
        }
        return $this->response(array_values($new_arr));
//
//        if (intval($data['unit_id']) !== 0) {
//            $res = AuthWxMenu::with(['wxMenu'])->where(['unit_id' => $data['unit_id']])->select()->toArray();
//            if (empty($res)) {
//                $find = AuthWxMenu::findWxMenu(intval($data['unit_id']), $this->search);
//                $arr = [];
//                foreach ($find as $key => $v) {
//                    $arr[] = $v['id'];
//                }
//                $wx_menu = AuthWxMenu::with(['wxMenu'])->whereIn('id', $arr)->order('id asc')->select()->filter(function (AuthWxMenu $authWxMenu) {
//                    if (!empty($authWxMenu->wxMenu)) {
//                        return true;
//                    }
//                })->toArray();
//                foreach ($wx_menu as $key => $val) {
//                    $new_arr[$val['wxMenu']['menu_type']]['title'] = WxMenu::$menu_type_arr[$val['wxMenu']['menu_type']];
//                    $new_arr[$val['wxMenu']['menu_type']]['child'][] = $val['wxMenu'];
//                }
//            } else {
//                foreach ($res as $key => $val) {
//                    $new_arr[$val['wxMenu']['menu_type']]['title'] = WxMenu::$menu_type_arr[$val['wxMenu']['menu_type']];
//                    $new_arr[$val['wxMenu']['menu_type']]['child'][] = $val['wxMenu'];
//                }
//            }
//        }else{
//            $wx_menu_id_arr = AuthWxMenu::column('wx_menu_id');
//            $res = WxMenu::whereIn('id',array_values(array_unique($wx_menu_id_arr)))->select()->toArray(); //去重查询
//            foreach ($res as $key=>$v){
//                $res[$key]['wxMenu'] = $v;
//                $new_arr[$res[$key]['wxMenu']['menu_type']]['title'] = WxMenu::$menu_type_arr[$v['menu_type']];
//                $new_arr[$res[$key]['wxMenu']['menu_type']]['child'][] = $v;
//            }
//        }
        return $this->response(array_values($new_arr));
    }

    /**
     * Notes:
     * User: liuhailong
     * DateTime: 2021/12/28 10:02
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 保存微信菜单
     */
    public function saveMenu()
    {
        $id = $this->request->param('id', 0);
        $data = $this->request->param();
        $data['uid'] = $this->request->uid; //用户id
        if (intval($id) === 0) {
            WxMenu::create($data);
        } else {
            $res = WxMenu::find($id);
            if (is_null($res)) {
                return $this->response(403, '数据不存在');
            }
            $res->save($data);
        }
        AdminLog::record(intval($id) === 0 ? '新增微信菜单' : '编辑微信菜单', $this->request->param(), $this->request->uid, $this->request->username);
        return $this->response();
    }

    /**
     * Notes:
     * User: liuhailong
     * DateTime: 2022/5/30 13:49
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 保存微信角色
     */
    public function saveWxMenuRole()
    {
        $data = $this->request->param();
        $data['rules'] = implode(',', $data['rules']);
        if (intval($data['id']) === 0) {
            $role = new AuthWxGroup();
        } else {
            $role = AuthWxGroup::find($data['id']);
        }
        $role->pid = $this->request->param('pid', 0);
        $role->name = $this->request->param('name');
        $role->rules = $data['rules'];
        $role->status = $this->request->param('status', 1);
        $role->save();
        return $this->response();
    }

    /**
     * Notes:
     * User: liuhailong
     * DateTime: 2022/5/30 13:50
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 获取微信角色列表
     */
    public function getWxRoleList(){
        $res = AuthWxGroup::select();
        return $this->response($res);
    }

    /**
     * Notes:
     * User: liuhailong
     * DateTime: 2022/5/30 14:09
     * @param $id
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException\
     * 删除微信角色
     */
    public function deleteWxRole($id)
    {
        $res = AuthWxGroup::find($id);
        if (is_null($res)) {
            return $this->response(403, '找不到该资源信息');
        }
        $res->delete();

        return $this->response();
    }


    /**
     * Notes:
     * User: liuhailong
     * DateTime: 2021/12/28 11:32
     * @param $id
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 删除菜单
     */
    public function deleteWxMenu($id)
    {
        $res = WxMenu::with(['wxMenuAuth'])->find($id);
        if (is_null($res)) {
            return $this->response(403, '找不到该资源信息');
        }
        $res->together(['wxMenuAuth'])->delete();
        AdminLog::record('删除微信菜单', $this->request->param(), $this->request->uid, $this->request->username);
        return $this->response();
    }

    /**
     * Notes:
     * User: liuhailong
     * DateTime: 2021/12/31 14:40
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 保存微信权限菜单
     */
    public function saveAuthWxMenu()
    {
        $data = $this->request->param();
        $res = AuthWxMenu::where(['unit_id' => $data['id']])->select();
        if (!$res->isEmpty()) {
            $res->delete();
        }
//        sort($data['rules']); //排序
        foreach ($data['rules'] as $key => $v) {
            AuthWxMenu::create([
                'unit_id' => $data['id'],
                'wx_menu_id' => $v
            ]);
        }
        AdminLog::record('添加微信全选菜单', json_encode($data), $this->request->uid, $this->request->username);
        return $this->response();
    }

    /**
     * Notes:
     * User: liuhailong
     * DateTime: 2022/4/20 16:44
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 保存题库设置
     */
    public function saveAuthWxStudy()
    {
        $data = $this->request->param();
        $res = AuthWxStudy::where(['unit_id' => $data['id']])->select();
        if (!$res->isEmpty()) {
            $res->delete();
        }
        sort($data['rules']); //排序
        foreach ($data['rules'] as $key => $v) {
            AuthWxStudy::create([
                'unit_id' => $data['id'],
                'learn_id' => $v
            ]);
        }
        AdminLog::record('添加微信全选题库设置', json_encode($data), $this->request->uid, $this->request->username);
        return $this->response();
    }

    /**
     * Notes:
     * User: liuhailong
     * DateTime: 2021/12/31 15:41
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 获取微信菜单权限读取
     */
    public function getAuthWxMenuList()
    {
        $group_id = $this->request->param('unit_id');
        $res = AuthWxMenu::field('wx_menu_id')->where(['unit_id' => $group_id])->select();
        $new_arr = [];
        foreach ($res as $key => $v) {
            $new_arr[] = $v['wx_menu_id'];
        }
        if (empty($new_arr)) {
            return $this->response(403, '暂无数据');
        } else {
            return $this->response($new_arr);
        }
    }

    /**
     * Notes:
     * User: liuhailong
     * DateTime: 2022/4/20 16:43
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 题库权限读取
     */
    public function getAuthWxStudyList()
    {
        $unit_id = $this->request->param('unit_id');
        $res = AuthWxStudy::field('learn_id')->where(['unit_id' => $unit_id])->select();
        $new_arr = [];
        foreach ($res as $key => $v) {
            $new_arr[] = $v['learn_id'];
        }
        if (empty($new_arr)) {
            return $this->response(403, '暂无数据');
        } else {
            return $this->response($new_arr);
        }
    }
}
